/*
 * @Description: 根据权限匹配路由并返回
 * @Author: 0001100986/LiuShuai
 * @Date: 2022-09-08 15:06:18
 * @LastEditTime: 2022-11-23 09:48:13
 * @LastEditors: 0001100986/LiuShuai
 */
export function routerMatch (permission, allowRouters) {
  return new Promise((resolve) => {
    const routers = [];
    function createRouter (permission) {
      permission.forEach((item) => {
        let { path, title, title_en } = item;
        let pathArr = path && path.split('/');

        if (pathArr) {
          path = pathArr[pathArr.length - 1];
        }

        if (item.children && item.children.length) {
          createRouter(item.children);
        }

        allowRouters.find((s) => {
          if (s.path === item.path) {
            s.meta.title = item.title;
            s.meta.title_en = item.title_en;
          }

          if (s.children) {
            s.children.find((y) => {
              let cPath = s.path + '/' + y.path;

              if (cPath === item.path) {
                y.meta.permission = item.permission;
                y.meta.title = title;
                y.meta.title_en = title_en;
                routers.push(s);
              }
            });
          } else {
            if (path && s.path === path) {
              s.meta.permission = item.permission;
              // eslint-disable-next-line no-undef
              y.meta.title = title;
              // eslint-disable-next-line no-undef
              y.meta.title_en = title_en;
              routers.push(s);
            }
          }
        });
      });
    }
    createRouter(permission);
    resolve(Array.from(new Set(routers)));
  });
}